﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using EZCRS.HCMS.Entity;
using GFrame.Shadow;
using GFrame.Shadow.Search;
using GFrame.Data;

namespace EZCRS.HCMS.SqlClient.AIR
{
    public class AirNotify
    {
        private ShadowSearch<HCRS_AIR_NOTIFY> _search;

        public AirNotify()
        {
            _search = new ShadowSearch<HCRS_AIR_NOTIFY>();

            _search.ConnectionName = MainBase.CONNECTION_STRING;
        }


        public int AddNotify(HCRS_AIR_NOTIFY airnotify)
        {
            Shadow<HCRS_AIR_NOTIFY> sha = new Shadow<HCRS_AIR_NOTIFY>(airnotify);
            sha.ConnectionName = MainBase.CONNECTION_STRING;

            sha.Save();

            return 0;
        }

        public int AddNotify(HCRS_AIR_NOTIFY airnotify, List<AIR_LOG_CODE> Codes)
        {

            Shadow<HCRS_AIR_NOTIFY> sha = new Shadow<HCRS_AIR_NOTIFY>(airnotify);
            sha.ConnectionName = MainBase.CONNECTION_STRING;

            sha.Save();

            foreach (AIR_LOG_CODE code in Codes)
            {
                HCRS_AIR_NOTIFY_DETAIL detail = new HCRS_AIR_NOTIFY_DETAIL();

                detail.NotifyID = airnotify.ID;
                detail.LogCodeID = code.ID;

                Shadow<HCRS_AIR_NOTIFY_DETAIL> dsha = new Shadow<HCRS_AIR_NOTIFY_DETAIL>(detail);
                dsha.ConnectionName = MainBase.CONNECTION_STRING;

                dsha.Save();
            }

            return 0;
        }

        public int DeleteNotify(HCRS_AIR_NOTIFY notify)
        {
            Shadow<HCRS_AIR_NOTIFY> sha = new Shadow<HCRS_AIR_NOTIFY>(notify);
            sha.ConnectionName = MainBase.CONNECTION_STRING;
            sha.Delete();

            return 0;
        }


        public List<HCRS_AIR_NOTIFY> GetItemsByPID(int PID, string Name, int PageIndex, int PageSize, out int RecordCount, out int PageCount)
        {
            ParamList pl = new ParamList();

            pl.Add("@pid", SqlDbType.Int, PID);

            ShadowRule rule = new ShadowRule();

            rule.PageSize = PageSize;
            rule.PageIndex = PageIndex;

            rule.OrderString = "ID DESC";

            string exp = "PID = @pid ";

            if (!string.IsNullOrEmpty(Name))
            {
                exp += "AND [Name] Like @name ";
                pl.Add("@name", System.Data.SqlDbType.VarChar, "%" + Name + "%");
            }


            List<HCRS_AIR_NOTIFY> lst = _search.Search(exp, rule, pl);

            RecordCount = rule.RecordCount;
            PageCount = rule.PageCount;

            return lst;

        }

        public HCRS_AIR_NOTIFY GetNotify(int ID)
        {
            ShadowSearch<HCRS_AIR_NOTIFY> search = new ShadowSearch<HCRS_AIR_NOTIFY>();
            search.ConnectionName = MainBase.CONNECTION_STRING;

            List<HCRS_AIR_NOTIFY> lst = search.Search("ID = " + ID.ToString());

            if (lst.Count > 0) return lst[0];

            return null;
        }

        public List<HCRS_AIR_NOTIFY_ADDRESS> GetNotifyAddress(int NotifyID)
        {
            ShadowSearch<HCRS_AIR_NOTIFY_ADDRESS> search = new ShadowSearch<HCRS_AIR_NOTIFY_ADDRESS>();
            search.ConnectionName = MainBase.CONNECTION_STRING;

            return search.Search("NOTIFY_ID = " + NotifyID.ToString());

        }

        public List<HCRS_AIR_NOTIFY_DETAIL> GetNotifyDetail(int NotifyID)
        {
            ShadowSearch<HCRS_AIR_NOTIFY_DETAIL> search = new ShadowSearch<HCRS_AIR_NOTIFY_DETAIL>();
            search.ConnectionName = MainBase.CONNECTION_STRING;

            return search.Search("NOTIFY_ID = " + NotifyID.ToString());

        }


        public bool InsertAddresseeBlockByChannel(int NotifyID, int ChannelID, string Protocol, int State)
        {
            using (SQLWriter sw = new SQLWriter())
            {

                sw.Begin(MainBase.CONNECTION_STRING);

                string strSQL = "INSERT INTO HCRS_AIR_NOTIFY_ADDRESSEE(NOTIFY_ID, Channel, IATA, Protocol, State) " +
                                "SELECT @notifyid, @channel, ta.IATA, @protocol, @state FROM HCRS_TA ta " +
                                "WHERE ta.IATA IN(SELECT IATA FROM HCRS_TA_CHANNEL WHERE Channel = @channel)" +
                                "AND (SELECT COUNT(1) FROM HCRS_AIR_NOTIFY_ADDRESSEE WHERE NOTIFY_ID = @notifyid AND IATA = ta.IATA AND Protocol = @protocol) = 0";

                ParamList pl = new ParamList();

                pl.Add("@notifyid", SqlDbType.Int, NotifyID);
                pl.Add("@channel", SqlDbType.Int, ChannelID);
                pl.Add("@state", SqlDbType.Int, State);
                pl.Add("@protocol", SqlDbType.VarChar, Protocol);

                sw.RunCommandText(strSQL, pl);

                if (sw.HasError)
                {
                    sw.RollBack();

                    return false;
                }
                else
                    sw.Commit();


                return true;
            }
        }

    }
}
